package com.zj.processors.booking;

import com.zj.bean.TableColumnInfo;
import com.zj.bean.TableInfo;
import com.zj.job.CodeSnippetJob;
import com.zj.processors.BaseProcessor;
import com.zj.processors.TemplateProcessor;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

@Component
public class CreateModuleProcessor extends BaseProcessor implements TemplateProcessor {

    private static final String baseDir="/Users/piza/work/code/lilin/hospital-booking/icl/icl-booking-web/src/main/";

    private CodeSnippetJob codeSnippetJob;
    private TableInfo tableInfo;


    @Override
    public String getName() {
        return "createModel";
    }

    @Override
    public String process(TableInfo tableInfo, CodeSnippetJob codeSnippetJob) {
        this.tableInfo=tableInfo;
        this.codeSnippetJob=codeSnippetJob;
        try {
            createFiles();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "ok";
    }



    private String getPackageName(String tableName) {

        if(tableName.contains("_")){
            tableName = tableName.substring(tableName.indexOf("_"), tableName.length());
        }
        tableName=tableName.replaceAll("_", "");
        return tableName.toLowerCase();

    }

    private void makedir(String dirPath) {
        File file = new File(dirPath);
        if (!file.exists()) {
            file.mkdir();
        }
    }
    public String createFiles() throws Exception {

        String packageName= getPackageName(tableInfo.getTableName());
        if(StringUtils.isNotEmpty(codeSnippetJob.getConfig().getModuleName())){
            packageName=codeSnippetJob.getConfig().getModuleName().trim();
        }
        String filePath = baseDir + "java/com/icl/" +packageName ;
        makedir(filePath);
        makedir(filePath+"/controller");
        makedir(filePath+"/dao");
        makedir(filePath+"/service");
        makedir(filePath+"/service/impl");
        makedir(filePath+"/model");

        VelocityContext context = new VelocityContext();
        String modelName=convertDomainName(tableInfo.getTableName());
        context.put("modulePackage", packageName);
        context.put("modelDesc", tableInfo.getDescription());
        context.put("modelName", modelName);
        context.put("modelProperty", lowFirstChar(modelName));

        writeFile(context,"booking-vm/serviceTemplate.vm",filePath +"/service/I"+modelName+"Service.java");
        writeFile(context,"booking-vm/serviceImplTemplate.vm",filePath+"/service/impl/"+modelName+"ServiceImpl.java");
        writeFile(context,"booking-vm/controllerTemplate.vm",filePath+"/controller/"+modelName+"Controller.java");
        writeFile(context,"booking-vm/mapperTemplate.vm",filePath+"/dao/"+modelName+"Dao.java");
        writeFile(context,"booking-vm/queryTemplate.vm",filePath+"/model/"+modelName+"Query.java");

        modelContext(context);
        writeFile(context,"booking-vm/modelTemplate.vm",filePath+"/model/"+modelName+".java");

        codeSnippetJob.log("task ok");
        return "ok";
    }

    private void modelContext(VelocityContext context) throws Exception{

        List<TableColumnInfo> colList = new ArrayList<>();
        for (TableColumnInfo tableColumnInfo : tableInfo.getTableColumnInfoList()) {
            String fieldName = lowFirstChar(convertDomainName(tableColumnInfo.getName()));
            if("createTime".equals(fieldName)){
                continue;
            }
            if("isDelete".equals(fieldName)){
                continue;
            }
            if("createUser".equals(fieldName)){
                continue;
            }
            if("updateTime".equals(fieldName)){
                continue;
            }
            if("updateUser".equals(fieldName)){
                continue;
            }
            tableColumnInfo.setName(fieldName);
            tableColumnInfo.setType(toJavaType(tableColumnInfo.getType()));
            colList.add(tableColumnInfo);
        }
        context.put("colList", colList);
    }

    private String toJavaType(String dbType) {
        dbType=dbType.toLowerCase();
        if (dbType.contains("(")) {
            dbType = dbType.substring(0, dbType.indexOf("("));
        }
        switch (dbType) {
            case "varchar":
                return "String";
            case "bigint":
                return "Long";
            case "datetime":
                return "Date";
            case "date":
                return "Date";
            case "char":
                return "String";
            case "int":
                return "Integer";
            case "text":
                return "String";
            case "tinyint":
                return "Byte";
        }

        return "unkown-"+dbType;
    }
}
